import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_auto_track/src/method/method_name.dart';

class AutoTrackPlugin {
  const AutoTrackPlugin._();

  /// MethodChannel.
  static const MethodChannel _methodChannel =
      MethodChannel('com.xq.auto.track.channel');

  /// 是否已初始化
  static bool isInitialized = false;

  /// 初始化方法。请保证其最先被调用。
  ///
  /// [appid] 控制台获取的 appid
  /// [channel] 渠道名称
  /// [enabledAutoTrack] 自动上报，默认开启
  /// [needEncrypt] 日志上报是否加密。用于debug情况可以抓包调试，默认开启
  /// [openDebugMode] 是否开启debug调试模式
  static Future<int> setup(
    String appid, {
    String? channel,
    bool enabledALink = true,
    bool enabledAutoTrack = true,
    bool needEncrypt = true,
    bool openDebugMode = false,
  }) async {
    final int ret =
        await _methodChannel.invokeMethod(MethodName.setup, <String, dynamic>{
      'appid': appid,
      if (channel != null)
        'channel': channel
      else
        'channel': Platform.isIOS ? 'appstore' : 'android',
      'enabledAutoTrack': enabledAutoTrack,
      'needEncrypt': needEncrypt,
      'openDebugMode': openDebugMode,
    });
    isInitialized = ret == 1;
    if (isInitialized) {
      _methodChannel.setMethodCallHandler(_methodCallHandler);
    }
    return ret;
  }

  /// 自定义上报
  /// [name] 自定义上报的名称
  /// [param] 上报的参数，可选
  static Future<void> event(
    String name, {
    Map<String, dynamic>? params,
  }) async {
    if (!isInitialized) {
      return;
    }
    _methodChannel.invokeMethod(MethodName.event, <String, dynamic>{
      'name': name,
      if (params != null) 'params': params,
    });
  }

  /// 获取android_id
  static Future<String?> fetchAndroidId() async {
    if (Platform.isAndroid) {
      return await _methodChannel.invokeMethod(MethodName.androidId);
    } else {
      return null;
    }
  }

  /// 获取sdk生成的bd_did
  static Future<String?> queryDeviceBDDID() async {
    return await _methodChannel.invokeMethod(MethodName.BDDID);
  }

  /// 获取android_OAID
  static Future<String?> fetchAndroidOAID() async {
    if (Platform.isAndroid) {
      return await _methodChannel.invokeMethod(MethodName.androidOAID);
    } else {
      return null;
    }
  }

  /// 获取clickID
  static Future<String?> fetchIOSClickID() async {
    if (Platform.isIOS) {
      return await _methodChannel.invokeMethod(MethodName.clickId);
    } else {
      return null;
    }
  }

  /// 获取android_imei
  static Future<String?> fetchAndroidIMEI() async {
    if (Platform.isAndroid) {
      return await _methodChannel.invokeMethod(MethodName.androidIMEI);
    } else {
      return null;
    }
  }

  /// 获取iOS的idfa
  /// [requestPermission] 是否在未授权时请求授权
  static Future<String?> fetchIOSIDFA({
    bool requestPermission = true,
  }) async {
    if (Platform.isAndroid) {
      return null;
    } else {
      return await _methodChannel
          .invokeMethod(MethodName.idfa, <String, dynamic>{
        'requestPermission': requestPermission,
      });
    }
  }

  /// 获取iOS设备状态，是否处于安全环境
  static Future<bool> checkDeviceIsSafe() async {
    if (Platform.isAndroid) {
      return true;
    } else {
      return await _methodChannel.invokeMethod(MethodName.safeDevice) ?? true;
    }
  }

  /// 处理原生端返回的数据
  static Future<dynamic> _methodCallHandler(MethodCall call) async {
    print('the native method name is ${call.method}');
  }

  /// 获取当前版本号
  static Future<String?> getPlatformVersion() async {
    final version =
        await _methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }
}
